package com.zwjlg.module_other;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.zwjlg.baselibrary.ARouterConstants;
import com.zwjlg.baselibrary.FixDexUtils;
import com.zwjlg.baselibrary.VideoCompressorUtil;
import com.zwjlg.module_user.service.UserInfoService;

import java.io.File;
import java.util.List;

/**
 * ================================================
 * <p>
 * Created by ligang on 2021/3/29
 * ================================================
 */
@Route(path = ARouterConstants.Activitys.OTHER_MAIN)
public class OtherMainActivity extends AppCompatActivity {
    @Autowired
    UserInfoService userInfoService;

    TextView tvMessage;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ARouter.getInstance().inject(this);
        setContentView(R.layout.other_activity_main);
        tvMessage = findViewById(R.id.tv_message_other);
        findViewById(R.id.btn_call_user).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tvMessage.setText(tvMessage.getText() + "\n" + userInfoService.getUser().getName());
                ARouter.getInstance().build(ARouterConstants.Activitys.OTHER_MARKDOWN)
                        .navigation();
            }
        });

        findViewById(R.id.btn_video_compress).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                PictureSelectionModel pictureSelectionModel = PictureSelector.create(this)
//                .openGallery(PictureMimeType.ofVideo())// 全部.PictureMimeType.ofAll()、图片.ofImage()、视频.ofVideo()、音频.ofAudio()
//                .maxSelectNum(maxNum)// 最大图片选择数量
//                .minSelectNum(1)// 最小选择数量
//                .imageSpanCount(4)// 每行显示个数
//                .selectionMedia(getSelectList())// 是否传入已选图片
//                .selectionMode(PictureConfig.MULTIPLE);// 多选 or 单选
                PictureSelector.create(OtherMainActivity.this)
                        .openGallery(PictureMimeType.ofVideo())
                        .maxSelectNum(1)
                        .minSelectNum(1)
                        .imageSpanCount(4)
                        .selectionMode(PictureConfig.SINGLE)
                        .forResult(100);
            }
        });

        findViewById(R.id.btn_fix).setOnClickListener(view -> {
//            HotFixEngine.copyDexFileToAppAndFix(this, "classes_fix.dex", true);
            FixDexUtils.loadFixDex(this, new File(Environment.getExternalStorageDirectory(), "classes_fix.dex").getAbsolutePath());
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && resultCode == RESULT_OK) {
            // 选择的结果
            List<LocalMedia> result = PictureSelector.obtainMultipleResult(data);
            if (result != null && result.size() > 0) {
                LocalMedia media = result.get(0);
                String path = media.getPath();
                VideoCompressorUtil.getInstance().compress(this, path, new VideoCompressorUtil.Callback() {
                    @Override
                    public void onCompressSuccess(String srcPath, String resultPath) {
                        tvMessage.post(() -> {
                            Toast.makeText(OtherMainActivity.this, srcPath + "\n" + resultPath, Toast.LENGTH_LONG).show();
                        });
                    }

                    @Override
                    public void onCompressFailed(String srcPath) {

                    }
                });
            }
        }
    }


}
